package com.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.biz.*;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.*;
import com.util.*;
import com.util.template.Template;
import com.util.template.TemplateParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController("apiWxTemplateController")
@RequestMapping("/api/sendTemplate")
public class WeiXinTemplateController {
    @Autowired
    private BillBiz billBiz;

    @Autowired
    private CustomerBiz customerBiz;

    @Autowired
    private HotelBiz hotelBiz;

    @Autowired
    private HotelHomeBiz hotelHomeBiz;

    @Autowired
    private HotelReservationBiz hotelReservationBiz;

    public String getToken() {
        String url = "https://api.weixin.qq.com/cgi-bin/token";
        String result = HttpRequest.sendGet(url, "grant_type=client_credential&appid=wx8a5bf19a3b8d5cd3&secret=52c497db3697593faf66936fd7e3f337");
        JSONObject object = JSON.parseObject(result);
        String Access_Token = object.getString("access_token");
        return Access_Token;
    }

    @RequestMapping("/billTemplate")
    public Result billTemplate(@RequestParam Integer billId) {
        Bill bill = billBiz.findById(billId);
        HotelReservation hotelReservation = hotelReservationBiz.findBy("billId", bill.getId());
        Hotel hotel = hotelBiz.findById(bill.getHotelId());
        HotelHome hotelHome = hotelHomeBiz.findById(bill.getHomeId());
        Customer customer = customerBiz.findById(bill.getCustomerId());

        Template template = new Template();
        template.setTemplate_id("fISiKy31W4h_HmIsJh3oLh2ib57G2o4N-udVcqfydOw");
        template.setTouser(customer.getOpenid());
        template.setPage("pages/tab/index/index");
        List<TemplateParam> paras = new ArrayList<TemplateParam>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        paras.add(new TemplateParam("thing1", hotel.getHotelName()));
        paras.add(new TemplateParam("thing8", hotelHome.getHomeName()));
        paras.add(new TemplateParam("date5", formatter.format(hotelReservation.getReserveStartTime())));
        paras.add(new TemplateParam("date6", formatter.format(hotelReservation.getReserveEndTime())));
        paras.add(new TemplateParam("phone_number10", hotel.getHotelTelNo()));
        template.setTemplateParamList(paras);
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", getToken());

        System.out.println(template.toJSON());
        JSONObject jsonResult = JSON.parseObject(PayUtil.httpRequest(requestUrl, "POST", template.toJSON()));
        if (jsonResult != null) {
            System.out.println(jsonResult);
            String errorCode = jsonResult.getString("errcode");
            String errorMessage = jsonResult.getString("errmsg");
            if (errorCode.equals("0")) {
                return ResultGenerator.genSuccessResult();
            } else {
                return ResultGenerator.genFailResult("订阅消息发送失败:" + errorCode + "," + errorMessage);
            }
        }
        return ResultGenerator.genFailResult("订阅消息发送异常");
    }
}
